<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/24/024
 * Time: 22:38
 */
namespace app\api\model;
use think\Model;
use think\facade\Url;
use phpqrcode\QRcode;

class MemberInfo extends Model
{
    
    
    /**
    * 会员注册时创建会员平台信息
    * @param mid int 会员id
    * @return bool
    */
    public function _addInfo($mid){
        $data = $this->where([['mid', '=', $mid]])->find();
        if(!empty($data)){
            $this->error = '该用户信息已存在';
            return false;
        }

        $insert_data = [
            'mid' => $mid,
        ];
        $ret = $this->save($insert_data);
        if(!$ret){
            $this->error = '用户平台信息创建失败';
            return false;
        }
        
        return true;
    }
    
    /**
    * 通过会员id获取平台信息
    */
    public function _getInfoByMid($mid){
        $data = $this->where([['mid', '=', $mid]])->find();
        
        return $data;
    }

    /**
    * 通过会员id获取平台信息
    */
    public function _getInfo($where){
        $data = $this->where($where)->find();
        
        return $data;
    }
    
    /**
    * 通过条件来修改参数
    * @param where array 条件
    * @param update array 更新的数据
    * @return bool
    */
    public function _update($where, $update){
        return $this->save($update, $where);
    }
    
    
    /**
    * 校验总金额是否达到实名验证需求
    * @param mid int 会员id
    * @param max int 需实名验证的上限金额
    * @return bool true:不用实名，false:需要实名
    */
    public function _checkIsVerifyRealname($mid, $max){
        $info = $this->_getInfoByMid($mid);
        if($info['total_balance'] >= $max){
            return false;
        }
        return true;
    }
    
    
    /**
    * 登录时修改连续登录天数
    * @param mid int 会员id
    * @return bool
    */
    public function _setLoginContinueNum($mid){
        $model = new Member;
        $member = $model->_getInfoById($mid);
        
        $memberInfo = $this->_getInfoByMid($mid);
        
        $last_time = strtotime(date("Y-m-d",$member['last_login_time']));
        $time = strtotime(date("Y-m-d")); //凌晨
        $ww = 48 * 3600;
        if($time - $last_time > $ww){ //若超过48小时则更新为0
            $update = [
                'login_continuity_num' => 0,
            ];
            $ret = $this->_update([['mid', '=', $mid]], $update);
            if(!$ret){
                $this->error = '连续天数重置失败';
                return false;
            }
        }else{
            if($time != $last_time){ //同一天之内不算连续登陆基础
                $update = [
                    'login_continuity_num' => $member['login_continuity_num'] + 1,
                    'login_num' => $member['login_num'] + 1,
                    'login_num_today' => 0,
                ];
                $ret = $this->_update([['mid', '=', $mid]], $update);
                if(!$ret){
                    $this->error = '连续天数重置失败';
                    return false;
                }
            }elseif($time == $last_time){ //同一天之内登陆次数更新
                $update = [
                    'login_num_today' => $member['login_num_today'] + 1,
                    'login_num' => $member['login_num'] + 1,
                ];
                $ret = $this->_update([['mid', '=', $mid]], $update);
                if(!$ret){
                    $this->error = '连续天数重置失败';
                    return false;
                }
            }
        }
        return true;
    }
    
}